package Service.impl;

import Mapper.OrderDetailsMapper;
import Mapper.OrderMapper;
import Mapper.RoomMapper;
import Mapper.RoomNumberMapper;
import Pojo.Order;
import Pojo.OrderDetails;
import Pojo.Room;
import Service.OrderDetailsService;
import Util.BaseMapper;

import java.util.Date;

public class OrderDetailsServiceImpl implements OrderDetailsService {
    private RoomMapper roomMapper= BaseMapper.getSqlSession().getMapper(RoomMapper.class);
    private OrderMapper orderMapper= BaseMapper.getSqlSession().getMapper(OrderMapper.class);
    private RoomNumberMapper roomNumberMapper=BaseMapper.getSqlSession().getMapper(RoomNumberMapper.class);
    private OrderDetailsMapper orderDetailsMapper=BaseMapper.getSqlSession().getMapper(OrderDetailsMapper.class);
    //价格计算
    @Override
    public Integer priceSum(Date startDate, Date endDate, Integer roomOfNum,Integer roomId) {
        Room room = roomMapper.RoomQueryById(roomId);//从数据库中查出传入对应id的房间的单价
        Integer sumResult=0;//总价
        //房间数量计算
        Integer roomPrice=room.getRoom_price();
        Integer roomSumPrice=roomPrice*roomOfNum;//价格乘以数量
        sumResult+=roomSumPrice;
        //时间差计算
        Long date=endDate.getTime() - startDate.getTime();
        Integer betweenDay=(int) (date/(24 * 60 * 60 * 1000));//强制类型转换，将long转换成int的包装类
        //最终价格计算
        sumResult=sumResult*betweenDay;
        System.out.println("总价格为:"+sumResult);
        return sumResult;
    }
    //订单详情新增
    @Override
    public Integer AddOne(OrderDetails orderDetails) {
        return orderDetailsMapper.AddOne(orderDetails);
    }

    @Override
    public Integer OrderAndRoomStatusUpdate(OrderDetails orderDetails) {
        Integer result=0;
        //修改详情表
        Integer integer=orderDetailsMapper.AddRoomNum(orderDetails);
        //修改order表
        Integer integer1= orderMapper.UpdateRoomId(orderDetails.getOrder_id());
        //修改房间号表
        Integer integer2=roomNumberMapper.UpdateStatusById(orderDetails.getRoom_number_id());
        return integer;
    }
}
